<template>
  <div class="home-container">
    <div class="welcome-card">
      <h1>欢迎使用审批系统</h1>
      <p>{{ user.name }}，您好！</p>
    </div>
    
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <div class="stat-card" @click="goToApprovalCenter('tasks')">
          <div class="stat-icon" style="background: #409EFF;">
            <i class="el-icon-s-order"></i>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ taskCount }}</div>
            <div class="stat-label">待办任务</div>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="stat-card" @click="goToApprovalCenter('applications')">
          <div class="stat-icon" style="background: #67C23A;">
            <i class="el-icon-document"></i>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ applicationCount }}</div>
            <div class="stat-label">我的申请</div>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="stat-card" @click="goToApprovalCenter('completed')">
          <div class="stat-icon" style="background: #E6A23C;">
            <i class="el-icon-circle-check"></i>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ completedCount }}</div>
            <div class="stat-label">已办任务</div>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="stat-card" @click="goToProcesses">
          <div class="stat-icon" style="background: #F56C6C;">
            <i class="el-icon-setting"></i>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ processCount }}</div>
            <div class="stat-label">可用流程</div>
          </div>
        </div>
      </el-col>
    </el-row>
    
    <div class="quick-actions">
      <h3>快速操作</h3>
      <el-row :gutter="20">
        <el-col :span="6" v-for="process in processes" :key="process.id">
          <el-card class="action-card" @click.native="startApproval(process.id)">
            <div class="action-icon">
              <i :class="process.icon || 'el-icon-document'"></i>
            </div>
            <div class="action-name">{{ process.processName }}</div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { getStartableProcesses } from '@/api/process'
import { getMyTasks, getMyApplications, getMyCompletedTasks } from '@/api/approval'

export default {
  name: 'Home',
  
  data() {
    return {
      processes: [],
      taskCount: 0,
      applicationCount: 0,
      completedCount: 0,
      processCount: 0
    }
  },
  
  computed: {
    user() {
      return this.$store.getters.user
    }
  },
  
  created() {
    this.loadData()
  },
  
  methods: {
    loadData() {
      getStartableProcesses().then(res => {
        this.processes = res.data
        this.processCount = res.data.length
      })
      
      getMyTasks().then(res => {
        this.taskCount = res.data.length
      })
      
      getMyApplications().then(res => {
        this.applicationCount = res.data.length
      })
      
      getMyCompletedTasks().then(res => {
        this.completedCount = res.data.length
      })
    },
    
    startApproval(processId) {
      this.$router.push(`/approval/start/${processId}`)
    },
    
    // 跳转到审批中心并切换到指定tab
    goToApprovalCenter(tab) {
      this.$router.push({
        path: '/approval-center',
        query: { tab: tab }
      })
    },
    
    // 跳转到流程管理
    goToProcesses() {
      this.$router.push('/approval/select')
    }
  }
}
</script>

<style scoped>
.home-container {
  padding: 20px;
}

.welcome-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  padding: 40px;
  border-radius: 10px;
  margin-bottom: 30px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.welcome-card h1 {
  font-size: 32px;
  margin-bottom: 10px;
}

.welcome-card p {
  font-size: 18px;
  opacity: 0.9;
}

.stats-row {
  margin-bottom: 30px;
}

.stat-card {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
}

.stat-icon i {
  font-size: 28px;
  color: #fff;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.quick-actions {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.quick-actions h3 {
  margin-bottom: 20px;
  color: #303133;
}

.action-card {
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
}

.action-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.action-icon {
  font-size: 48px;
  color: #409EFF;
  margin-bottom: 10px;
}

.action-name {
  font-size: 16px;
  color: #303133;
}
</style>

